package org.huzhp.authorization;

import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientRegistrationException;
import org.springframework.security.oauth2.provider.client.BaseClientDetails;
import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService;

import javax.sql.DataSource;

public class BaseClientDetailService extends JdbcClientDetailsService {

    private PasswordEncoder passwordEncoder;

    public BaseClientDetailService(DataSource dataSource) {
        super(dataSource);
    }

    public BaseClientDetailService(DataSource dataSource, PasswordEncoder passwordEncoder) {
        super(dataSource);
        this.passwordEncoder = passwordEncoder;
    }


    @Override
    public ClientDetails loadClientByClientId(String clientId) throws ClientRegistrationException {
        BaseClientDetails clientDetails = (BaseClientDetails) super.loadClientByClientId(clientId);
        clientDetails.setClientSecret(passwordEncoder.encode(clientDetails.getClientSecret()));
        return clientDetails;
    }

}
